<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片区域 -->
      <el-card class="tree-card">
        <!-- 头部区域 -->
        <TreeTools :tree-node="company" is-root @addDepts="addDepts" />
        <!-- 树形列表区域 -->
        <el-tree :data="departs" :props="defaultProps">
          <!-- 插槽内容-单条数据显示组件 -->
          <TreeTools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
        </el-tree>
        <!-- 添加/编辑部门对话框组件 -->
        <AddDept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
      </el-card>
    </div>
  </div>
</template>

<script>
import TreeTools from '@/views/departments//components/tree-tools.vue'
import AddDept from '@/views/departments//components/add-dept.vue'
import { getDepartments } from '@/api/departments'
// 转化树形结构
import { tranListToTreeData } from '@/utils'
export default {
  components: {
    TreeTools,
    AddDept
  },

  data() {
    return {
      // 树形列表数据
      departs: [],
      // 树形列表配置对象
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 公司信息
      company: { name: '全球顶尖开发团队', manager: '负责人', id: '' },
      // 对话框显示与隐藏的标识
      showDialog: false,
      // 当前点击的树形结构数据
      node: null
    }
  },

  created() {
    // 调用请求组织架构方法
    this.getDepartments()
  },

  methods: {
    // 请求组织架构信息
    async getDepartments(flag) {
      const result = await getDepartments()
      this.company.name = result.companyName
      this.departs = tranListToTreeData(result.depts, '')
    },

    // 接收当前点击添加的部门数据
    addDepts(node) {
      this.showDialog = true
      this.node = node
    },

    // 接收当前点击编辑的部门数据
    editDepts(node) {
      this.showDialog = true
      this.node = node
      // 调用编辑对话框方法请求部门信息
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
